<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.user_id" placeholder="用户ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.post_id" placeholder="动态ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称" min-width="100" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.nickname }}</a>
        </template>
      </el-table-column>
      <el-table-column label="孔明灯ID" min-width="100" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.lantern_id }}</a>
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="230" align="center">
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否顶级" min-width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.parent_id | formatParent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="created_at" sortable="custom" align="center" min-width="120">
        <template slot-scope="{row}">
          <span>{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <a v-if="row.status === 1" class="click_a" @click="agreePost(row.id,2)">
            删除
          </a>
          <a v-if="row.status === 2" class="click_a_error" @click="agreePost(row.id,1)">
            恢复
          </a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { lanternCommentList, lanternCommentAudit } from '@/api/lantern'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    },
    formatParent: function(parent) {
      return parent > 0 ? '否' : '是'
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        sort: 'desc',
        sort_field: 'id',
        user_id: '',
        post_id: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      lanternCommentList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    },
    agreePost(id, status) {
      lanternCommentAudit({ id: id, status: status }).then((response) => {
        if (response.code === 1) {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
        }
      })
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
.click_a_error {
  color: #ff4949;
  padding-right: 5px;
}
</style>
